package com.wing.dict.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.dict.model.entity.Dict;
import com.wing.dict.model.entity.DictLabel;
import com.wing.dict.service.DictLabelService;
import com.wing.dict.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController("dictController")
@Api(value = "系统字典模块API", tags = "#/dict/dict")
@RequestMapping("dict/admin/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @Autowired
    private DictLabelService dictLabelService;

    /**
     * 字典类型列表
     */
    @ApiOperation(value = "字典类型", notes = "列表")
    @GetMapping("/list")
    PageResult<Dict> list(@RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(required = false) String name
            , @RequestParam(required = false) String type
            , @RequestParam(required = false) String dictStatus) {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(name)) {
            dictQueryWrapper.like("name", name);
        }
        if (StringUtils.isNotBlank(type)) {
            dictQueryWrapper.like("type", type);
        }
        if (StringUtils.isNotBlank(dictStatus)) {
            dictQueryWrapper.eq("status", dictStatus);
        }
        Page<Dict> pg = new Page<>(page, limit);
        IPage<Dict> list = dictService.page(pg, dictQueryWrapper);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    /**
     * 字典类型下拉框列表
     */
    @ApiOperation(value = "字典类型", notes = "下拉框列表")
    @GetMapping("/select")
    JsonResult select() {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("status", "0");
        return JsonResult.success("查询成功",dictService.list(dictQueryWrapper));
    }

    @ApiOperation(value = "添加字典类型", notes = "添加")
    @PostMapping("/add")
    public JsonResult add(@Validated Dict dict) {
        dict.setCreateTime(new Date());
        if (dictService.save(dict)) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "更新字典类型", notes = "更新")
    @PutMapping("/update")
    public JsonResult update(@Validated Dict dict) {
        if (dictService.updateById(dict)) {
            return JsonResult.success("更新成功");
        }
        return JsonResult.fail("更新失败");
    }

    @ApiOperation(value = "删除字典类型", notes = "删除")
    @DeleteMapping("/del/{id}")
    public JsonResult del(@PathVariable("id") Long id) {
        Dict dict = dictService.getById(id);
        QueryWrapper<DictLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_type",dict.getType());
        List<DictLabel> dictLabelList = dictLabelService.list(queryWrapper);
        if(!dictLabelList.isEmpty() && dictLabelList.size() > 0){
            return JsonResult.fail("字典类型["+dict.getName()+"]存在标签，不允许删除");
        }
        if (dictService.removeById(id)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }

}
